package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.SalesPrizesDTO;
import com.financing.wap.dto.SalesPrizesLogDTO;
import com.financing.wap.dto.SalesPrizesPeopleDTO;
import com.financing.wap.service.ISalesPrizesLogService;
import com.financing.wap.service.ISalesPrizesPeopleService;
import com.financing.wap.service.ISalesPrizesService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.web.controller.BaseController;
/***
 * 抽奖 销售
 * @author zhangguojing
 *
 */
@Controller("salesPrizesController")
@RequestMapping("/api/salesPrizes")
public class SalesPrizesController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private ISalesPrizesService salesPrizesService;
	
	@Autowired
	private ISalesPrizesLogService salesPrizesLogService;
	
	@Autowired
	private ISalesPrizesPeopleService salesPrizesPeopleService;
	
	/***
	 * 奖项配置
	 * @param request
	 * @param salesPrizes
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listSalesPrizes")
	@ResponseBody
	public ResultMap list(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query salesPrizes list begin .......");
			SalesPrizesDTO salesPrizes=new SalesPrizesDTO();
			salesPrizes.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			
			List<SalesPrizesDTO> salesPrizesList = salesPrizesService.queryList(salesPrizes);
			result.setAttribute("list", salesPrizesList);
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			
			logger.info("query salesPrizes list end .......");
		}catch(Exception e){
			log.error("query salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 当期已中奖名单
	 * @param request
	 * @param salesPrizes
	 * @param pager
	 * @return
	 */
	@RequestMapping("/salesPrizesPeopleList")
	@ResponseBody
	public ResultMap salesPrizesPeopleList(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query salesPrizesPeopleList begin .......");
			
			List<Map<String,String>> salesPrizesList = salesPrizesService.salesPrizesPeopleList();
			result.setAttribute("list", salesPrizesList);
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			
			logger.info("query salesPrizesPeopleList end .......");
		}catch(Exception e){
			log.error("query salesPrizesPeopleList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 是否在名单内
	 * @param request
	 * @return
	 */
	@RequestMapping("/salesPrizesInList")
	@ResponseBody
	public ResultMap salesPrizesInList(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info(" salesPrizesInList begin .......");
			
			//获取抽奖人姓名
			String loginName = SessionUtil.getSession().getUser().getLoginName();
			
			SalesPrizesPeopleDTO salesPrizesPeople=new SalesPrizesPeopleDTO();
			salesPrizesPeople.setSalesPhone(loginName);
			salesPrizesPeople.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			SalesPrizesPeopleDTO people = salesPrizesPeopleService.queryListGetFirst(salesPrizesPeople);
			if(people==null||people.getId()==null){
				//不在名单内 直接返回
				result.setStatus(ResultMap.STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_4108);
			}else{
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			}
			logger.info("salesPrizesInList end .......");
		}catch(Exception e){
			log.error(" salesPrizesInList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/***
	 * 抽奖
	 * @param request
	 * @return
	 */
	@RequestMapping("/salesPrizes")
	@ResponseBody
	public ResultMap salesPrizes(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info(" salesPrizes begin .......");
			
			//获取抽奖人姓名
			String loginName = SessionUtil.getSession().getUser().getLoginName();
			
			salesPrizesService.salesPrizes(loginName, result);
			
			logger.info("salesPrizes end .......");
		}catch(Exception e){
			log.error(" salesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	/***
	 * 已抽奖列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/salesPrizesList")
	@ResponseBody
	public ResultMap salesPrizesList(HttpServletRequest request,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info(" salesPrizesList begin .......");
			
			//获取抽奖人姓名
			String loginName = SessionUtil.getSession().getUser().getLoginName();
			SalesPrizesLogDTO salesPrizesLog=new SalesPrizesLogDTO();
			salesPrizesLog.setPrizesPeoplePhone(loginName);
			salesPrizesLog.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			int pageNo=pager.getPageNo();
			salesPrizesLog.setPageNo(pageNo);
			int pageSize=pager.getPageSize();
			salesPrizesLog.setPageSize(pageSize);
			//获取最新一期的抽奖日志
			salesPrizesLog.setPrizesDate(new DateUtil().getDate());
			int count = salesPrizesLogService.queryListCount(salesPrizesLog);
			pager.setTotalCount(count);
			if(count>0){
				List<SalesPrizesLogDTO> queryList = salesPrizesLogService.queryList(salesPrizesLog);
				result.setAttribute("list", queryList);
			}else{
				result.setAttribute("list", Collections.EMPTY_LIST);
			}
			result.setAttribute("pager", pager);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			logger.info("salesPrizesList end .......");
		}catch(Exception e){
			log.error(" salesPrizesList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
